function HandlePageWage(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._createElements();
};
HandlePageWage.prototype._createElements = function() {
  var _self = this;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();
  this._search = DlmanageUtils.acctExpenseSearch();

  tr = this._gui.mains.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.backgroundColor = '#fff';
  td.colSpan = 12;
  td.appendChild(DOMUtils.getLoadingImage());
  
  this._loadData();
};

HandlePageWage.prototype._loadData = function() {
  this._updateds = null;
  this._expenses = null;
  this._parameters = null;
  this._payees = null;
  this._particulars = null;
  this._retrieveUpdates();
  this._retrieveExpenses(1);
  this._retrievePayees();
  this._retrieveParticulars();
};

HandlePageWage.prototype._verifyData = function() {
  if (this._updateds && this._expenses && this._payees && this._particulars) {
    this._updateElements();
  }
};

HandlePageWage.prototype._retrieveUpdates = function() {
  var _self = this;
  var cond = 'WHERE (e.status = 2 OR e.status = 3) AND (e.particular LIKE \'%Wage%\')';
  var args = 'c=' + cond + '&operator=' + JSON.stringify(this._operator);
  new RequestUtils()._mysql('wageupdated', args, function(result, params) { _self._updateds = result.data;
                                                                            _self._verifyData.call(_self);
                                                                          }, null);
};

HandlePageWage.prototype._retrieveExpenses = function(page, condition, from, to, pfrom, pto, pos) {
  var _self = this;
  var cond = '(d.status = 1)' + ' AND (d.particular LIKE \'%Wage%\')';
  var args = ((condition) ?  'c=' + condition + ' AND ' + cond: 'c=' + cond) + '&p=' + page  + ((from) ?  '&from=' + from : '') + ((to) ?  '&to=' + to : '') + '&s=' + DlmanageUtils.number + ((pfrom) ?  '&pfrom=' + pfrom : '') + ((pto) ?  '&pto=' + pto : '');
  new RequestUtils()._custom('acctexpenses', args, function(result, params) { _self._expenses = result.data;
                                                                              _self._parameters = result;
                                                                              _self._verifyData.call(_self);
                                                                            }, { pos: (pos) ? pos : null });
};

HandlePageWage.prototype._retrievePayees = function() {
  var _self = this;
  new RequestUtils()._read('ipayee', null, null, null, null, 'd.name', null, function(result, params) { _self._payees = result.data;
                                                                                                        _self._verifyData.call(_self);
                                                                                                      }, null);
};

HandlePageWage.prototype._retrieveParticulars = function() {
  var _self = this;
  new RequestUtils()._read('iparticular', null, null, null, null, 'd.name', null, function(result, params) { _self._particulars = result.data;
                                                                                                             _self._verifyData.call(_self);
                                                                                                           }, null);
};

HandlePageWage.prototype._updateElements = function() {
  var _self = this;
  DlmanageUtils.clear(this._gui.search);
  DOMUtils.removeChildElements(this._gui.pagination);
  DOMUtils.removeTableRows(this._gui.mains, 1);
  DOMUtils.removeTableRows(this._gui.table, 1);
  DOMUtils.removeChildElements(this._gui.more);
  var unique = MiscUtils.unique();
  DOMUtils.removeChildElements(this._gui.checkAll);
  this._selects = [];
  
  if (this._updateds.length == 0) {
    tr = this._gui.table.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 6;
    td.style.height = '25px';
    td.style.backgroundColor = '#ffffff';
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #b4b4b4';
    td.style.borderLeft = 'solid 1px #b4b4b4';
    td.style.borderBottom = 'solid 1px #b4b4b4';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var j = 0, jl = this._updateds.length; j < jl; j++) {
    var object = this._updateds[j];
    if (j < 5) {
      tr = this._gui.table.insertRow(-1);
      tr.style.backgroundColor = (j % 2 == 0) ? '#fff' : '#ccc';
      tr.style.height = '24px';
      new TableRowLastUpdatedWage(tr, object, {user: this._user, date: this._date} );
    }
  }
  if (this._updateds.length > 5) {
    var a = document.createElement('a');
    a.appendChild(document.createTextNode('Find all'));
    a.href = '#';
    a.onclick = function() { var w = window.open('?p=pageacct&t=pagelastupdatedwage', 'Last Updated', 'width=1024,height=768,scrollbars=1,location=0', true);
                             w.focus();
                             return false;
                           };
    this._gui.more.appendChild(a);
  }
  
  
  this._gui.modify.onclick = function() { if (_self._selects.length == 0) {
                                          window.alert('No Items Selected');
                                          return;
                                        }
                                        var empty = false; expenses = [];
                                        for(var i = 0, il = _self._selects.length; i < il; i++) {
                                          var object = _self._selects[i];
                                          if (object.method == '') {
                                            empty = true;
                                          } else {
                                            if (object.method == 'C') {
                                              object.status = 2;
                                            } else {
                                              object.status = 3;
                                            }
                                            object.confirmOperator = _self._operator.account;
                                            object.confirmDate = _self._now;
                                            expenses[expenses.length] = object;
                                          }
                                        }
                                        if (empty) {
                                          window.alert('METHOD IS NOT EMPTY');
                                          return;
                                        }
                                        new RequestUtils()._write('expense', expenses, [], function() { _self._retrieveUpdates();_self._retrieveExpenses(_self._parameters.page, _self._parameters.condition); }, { pos: DOMUtils.findPos(this) });
                                      };

  if (this._expenses.length == 0) {
    tr = this._gui.mains.insertRow(-1);
    tr.className = 'roweven';
    td = tr.insertCell(-1);
    td.colSpan = 12;
    td.style.height = '24px';
    td.style.borderRight = 'solid 1px #694247';
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
  }

  for (var i = 0, il = this._expenses.length; i < il; i++) {
    var expense = this._expenses[i];
    tr = this._gui.mains.insertRow(-1);
    tr.className = (tr.rowIndex % 2 == 0) ? 'roweven' : 'rowodd';
    
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #885b5d';
    td.style.textAlign = 'center';
    var check = DOMUtils.createInput(null, unique + '_' + i, 'checkbox', '', null);
    check._object = expense;
    check.onclick = function() { if (this.checked ==  true) {
                                   _self._selects[_self._selects.length] = this._object;
                                 } else {
                                   MiscUtils.remove(_self._selects, this._object, 'trackId');
                                 }
                               };
    td.appendChild(check);
    
    td = tr.insertCell(-1);
    td.style.height = '25px';
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #885b5d';
    td.appendChild(document.createTextNode(SimpleDate.format(expense.createdDate)));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #885b5d';
    td.appendChild(document.createTextNode(SimpleDate.format(expense.payDate)));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 10px';
    td.style.borderRight = 'solid 1px #885b5d';
    td.appendChild(document.createTextNode(expense.payee));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 10px';
    td.style.borderRight = 'solid 1px #885b5d';
    td.appendChild(document.createTextNode(expense.particular));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 10px';
    td.style.borderRight = 'solid 1px #885b5d';
    td.appendChild(document.createTextNode('$' + expense.amount));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #885b5d';
    var c = DOMUtils.createInput(null, null, 'radio', '', null);
    expense.method = '';
    c.name = 'radio' + i;
    c._expense = expense;
    c._index = i;
    c.style.defaultChecked = false;
    c.onclick = function() { if (this.checked == true) {
                               this._expense.method = 'C';
                               this._expense.chequeNumber = '';
                               document.getElementById('_cheque_input' + this._index).disabled = true;
                               document.getElementById('_cheque_input' + this._index).value = '';
                             }
                           };
    td.appendChild(c);
    td.appendChild(document.createTextNode('C'));

    var b = DOMUtils.createInput(null, null, 'radio', '', null);
    b.name = 'radio' + i;
    b._expense = expense;
    b._index = i;
    b.style.defaultChecked = false;
    b.onclick = function() { if (this.checked == true) {
                               this._expense.method = 'B';
                               this._expense.chequeNumber = '';
                               document.getElementById('_cheque_input' + this._index).disabled = true;
                               document.getElementById('_cheque_input' + this._index).value = '';
                             }
                           };
    td.appendChild(b);
    td.appendChild(document.createTextNode('B'));
    var q = DOMUtils.createInput(null, null, 'radio', '', null);
    q.name = 'radio' + i;
    q._expense = expense;
    q.style.defaultChecked = false;
    q._index = i;
    q.onclick = function() { if (this.checked == true) {
                               this._expense.method = 'Q';
                               document.getElementById('_cheque_input' + this._index).disabled = false;
                             }
                           };
    td.appendChild(q);
    td.appendChild(document.createTextNode('Q'));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #694247';
    td.style.textAlign = 'center';
    var chequeNo = DOMUtils.createInput(null, null, 'text', '', null);
    chequeNo.style.width = '65px';
    chequeNo.style.borderRight= 'solid 1px #fff';
    chequeNo.style.borderBottom=  'solid 1px #fff';
    chequeNo.style.borderLeft= 'solid 1px #d2c4b9';
    chequeNo.style.borderTop= 'solid 1px #d2c4b9';
    chequeNo.id = '_cheque_input' + i;
    chequeNo.disabled = true;
    chequeNo._expense = expense;
    chequeNo.onchange = function() { this._expense.chequeNumber = this.value; };
    td.appendChild(chequeNo);
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #885b5d';
    var check = DOMUtils.createInput(null, unique + '_' + i, 'checkbox', '', null);
    check._expense = expense;
    check.onclick = function() { if (this.checked == true) {
                                   this._expense.ng = true;
                                 } else {
                                   this._expense.ng = false;
                                 }
                               };
    td.appendChild(check);

    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #885b5d';
    var check = DOMUtils.createInput(null, unique + '_' + i, 'checkbox', '', null);
    check._expense = expense;
    check.onclick = function() { if (this.checked == true) {
                                   this._expense.ne = true;
                                 } else {
                                   this._expense.ne = false;
                                 }
                               };
    td.appendChild(check);

    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #885b5d';
    td.style.textAlign = 'center';
    var note = DOMUtils.createInput(null, null, 'text', expense.note, null);
    note.style.width = '110px';
    note.style.borderRight= 'solid 1px #fff';
    note.style.borderBottom=  'solid 1px #fff';
    note.style.borderLeft= 'solid 1px #d2c4b9';
    note.style.borderTop= 'solid 1px #d2c4b9';
    note._expense = expense;
    note.onchange = function() { this._expense.note = this.value; };
    td.appendChild(note);
    
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #885b5d';
    td.style.textAlign = 'center';
    var remove = document.createElement('img');
    remove.src = 'images/common/delete.png';
    remove.style.cursor = 'pointer';
    remove._expense = expense;
    remove.onclick = function() { var pos = DOMUtils.findPos(this);
                                  var div = document.createElement('div');
                                  var expense = this._expense
                                  var func1 = function() { expense.status = -1;
                                                           expense.cancelledDate = _self._now;
                                                           expense.cancelledOperator = _self._operator.account;
                                                           expense.cancelledMessage = expense._text;
                                                           new RequestUtils()._write('expense', [expense], [], function() { _self._retrieveExpenses(_self._parameters.page, _self._parameters.condition); }, { pos: pos });
                                                           _self._popupBox2._close();
                                                         };
                                  var func2 = function() { _self._popupBox2._close(); };
                                  MiscUtils.dialog(_self._popupBox2, pos, div, func1, func2, {t: 'Cancel Reason'});
                                  new DialogInput(div, expense, { title: '' });
                                  return false;
                                };
    td.appendChild(remove);
  }
  
  this._gui.checkAll.checked = false;
  this._gui.checkAll._unique = unique;
  this._gui.checkAll.onclick = function() { if (this.checked == true) {
                                             for (var i = 0, il = _self._expenses.length; i < il; i++) {
                                                var object = _self._expenses[i];
                                                if (document.getElementById(this._unique + '_' + i) && !document.getElementById(this._unique + '_' + i).checked) {
                                                  _self._selects[_self._selects.length] = object;
                                                  document.getElementById(this._unique + '_' + i).checked = true;
                                                }
                                              }
                                            } else {
                                              for (var i = 0, il = _self._expenses.length; i < il; i++) {
                                                var object = _self._expenses[i];
                                                if (document.getElementById(this._unique + '_' + i)) {
                                                  MiscUtils.remove(_self._selects, object, 'trackId');
                                                  document.getElementById(this._unique + '_' + i).checked = false;
                                                }
                                              }
                                            }
                                          };
  
  Pagination.makePagedResults(this._gui.pagination, this._parameters.page, this._parameters.total, this._parameters.size, function(page, condition) { _self._retrieveExpenses.call(_self, page, _self._parameters.condition); }, this, document);
  
  this._expenseSearch(this._gui, function(condition, from, to, pfrom, pto, pos) { _self._retrieveExpenses.call(_self, 1, condition, from, to, pfrom, pto, pos); });
};

HandlePageWage.prototype._expenseSearch = function(gui, callbackFunc) {
  var _self = this;
  this._callbackFunc = callbackFunc;
  gui.search.cdateFrom.value = (this._search.cdateFrom) ? SimpleDate.format(this._search.cdateFrom) : '';
  gui.search.cdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.cdateFrom, function(sd) { _self._search.cdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  gui.search.cdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                  }
                                                };
  gui.search.cdateTo.value = (this._search.cdateTo) ? SimpleDate.format(this._search.cdateTo) : '';
  gui.search.cdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.cdateTo, function(sd) { _self._search.cdateTo = sd; context.focus(); }, false);
                                            return false;
                                         };
  gui.search.cdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                               }
                                             };
                                             
  gui.search.pdateFrom.value = (this._search.pdateFrom) ? SimpleDate.format(this._search.pdateFrom) : '';
  gui.search.pdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.pdateFrom, function(sd) { _self._search.pdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  gui.search.pdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                  }
                                                };
  gui.search.pdateTo.value = (this._search.pdateTo) ? SimpleDate.format(this._search.pdateTo) : '';
  gui.search.pdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.pdateTo, function(sd) { _self._search.pdateTo = sd; context.focus(); }, false);
                                            return false;
                                          };
  gui.search.pdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                }
                                              };

  gui.search.payee.options[gui.search.payee.options.length] = new Option('');
  for (var i = 0, il = this._payees.length; i < il; i++) {
    var payee = this._payees[i];
    var option = new Option(payee.name);
    gui.search.payee.options[gui.search.payee.options.length] = option;
    if (option.text == this._search.payee){
      gui.search.payee.selectedIndex = gui.search.payee.options.length - 1;
    }
  }
  if (this._payees.length > 0) {
    this._search.payee = gui.search.payee.options[gui.search.payee.selectedIndex].text;
    gui.search.payee.onchange = function() { _self._search.payee = this.options[this.selectedIndex].text; };
  }
  
  gui.search.particular.options[gui.search.particular.options.length] = new Option('');
  for (var i = 0, il = this._particulars.length; i < il; i++) {
    var particular = this._particulars[i];
    var option = new Option(particular.name);
    gui.search.particular.options[gui.search.particular.options.length] = option;
    if (option.text == this._search.particular){
      gui.search.particular.selectedIndex = gui.search.particular.options.length - 1;
    }
  }
  if (this._particulars.length > 0) {
    this._search.particular = gui.search.particular.options[gui.search.particular.selectedIndex].text;
    gui.search.particular.onchange = function() { _self._search.particular = this.options[this.selectedIndex].text; };
  }
  
  gui.search.amount.value = this._search.amount;
  gui.search.amount.onkeydown = function() { _self._search.amount = this.value; };
  gui.search.amount.onchange = function() { _self._search.amount = this.value; };
  gui.search.amount.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                               if (code == 13) {
                                                 _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                               }
                                             };
 this._search.note = gui.search.note.value;
 gui.search.note.value = this._search.note;
 gui.search.note.onkeydown = function() { _self._search.note = this.value; };
 gui.search.note.onchange = function() { _self._search.note = this.value; };
 gui.search.note.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                             if (code == 13) {
                                               _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                             }
                                           };
 gui.search.note.onkeyup = function() { if (gui.search.note.value.length == 0) {
                                          _self._popupBox._close();
                                        } else {
                                          var div = document.createElement('div');
                                          var pos1 = DOMUtils.findPos(gui.search.note);
                                          var pos = [pos1[0] + 1, pos1[1] + 82];
                                          var query = "SELECT e.note FROM ns_Expense as e WHERE 1 = 1 AND " + '((e.note LIKE \'%' + gui.search.note.value + '%\'))' + " ";
                                          MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                          new DialogSearchResult(div, gui.search.note, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._updateElements();}, width: 195});
                                        }
                                      };
  gui.search.search.onclick = function() { _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                         };
};

HandlePageWage.prototype._toString = function(search) {
  var str = '1 = 1';
  
  str += (search.payee == '') ? '' : ' AND (d.payee LIKE \'%' + search.payee + '%\')';
  str += (search.particular == '') ? '' : ' AND (d.particular LIKE \'%' + search.particular + '%\')';
  str += (search.amount == '') ? '' : ((isNaN(search.amount)) ? ' AND 1 = 0' : ' AND (d.amount=\'' + search.amount + '\')');
  str += (search.note == '') ? '' : ' AND (d.note LIKE \'%' + search.note + '%\')';
  
  return str;
};

HandlePageWage.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePageWage.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePageWage.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};